package com.xinggq.pay.service.schedule;

import com.xinggq.pay.service.service.AppCallbackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 回调重试定时任务
 * @author xinggq
 * @since 2025/9/16
 */
@Slf4j
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "app.callback.retry.enabled", havingValue = "true", matchIfMissing = true)
public class CallbackRetryScheduler {

    private final AppCallbackService appCallbackService;

    /**
     * 每5分钟执行一次重试任务
     */
    @Scheduled(fixedRate = 300000) // 5分钟
    public void retryFailedCallbacks() {
        log.info("开始执行回调重试任务");
        try {
            appCallbackService.retryFailedCallbacks();
        } catch (Exception e) {
            log.error("回调重试任务执行失败", e);
        }
        log.info("回调重试任务执行完成");
    }
}